import {
	createApp
} from 'vue';
import App from './App.vue';
import router from './router';
import 'font-awesome/css/font-awesome.min.css';
import axios from 'axios';
import qs from 'qs';
import {
	getCurDate,
	setSessionStorage,
	getSessionStorage,
	removeSessionStorage,
	setLocalStorage,
	getLocalStorage,
	removeLocalStorage,
} from './common.js';

const app = createApp(App);
app.use(router);
app.config.productionTip = false;

// 设置 axios 基础 URL 部分
axios.defaults.baseURL = 'http://localhost:14000/';
// 设置响应拦截器
axios.interceptors.response.use(function(response){
	if(response.data.code==403){
		// location.href = '/error403';
		console.log("403" + "  ")
		console.log(response.data)
	}
	return response;
},function(error){
	console.log(error);
	return Promise.reject(error);
});

app.config.globalProperties.$qs = qs;
app.config.globalProperties.$axios = axios;
app.config.globalProperties.$getCurDate = getCurDate;
app.config.globalProperties.$setSessionStorage = setSessionStorage;
app.config.globalProperties.$getSessionStorage = getSessionStorage;
app.config.globalProperties.$setLocalStorage = setLocalStorage;
app.config.globalProperties.$getLocalStorage = getLocalStorage;
app.config.globalProperties.$removeLocalStorage = removeLocalStorage;
app.config.globalProperties.$removeSessionStorage = removeSessionStorage;

router.beforeEach((to, from, next) => {
    let user = sessionStorage.getItem('user');
	let business=sessionStorage.getItem('business')
    // 定义不需要登录就可以访问的路由路径
    const publicPaths = [
        '/',
        '/index',
        '/businessList',
        '/businessInfo',
        '/login',
        '/register',
        '/my',
		//'/reset',
        '/foodadmin', // 查看食品列表的路径
        '/add-food',  // 添加食品的路径
        '/edit-food',  // 编辑食品的路径，不包含具体的 foodId
		'/error403',
		'/comment'
    ];
	if(to.path=='/reset'){
		if(user==null&&business==null){
			next('/login');
		}
		else{
			next();
		}
	}
	if(to.path=='/wallet'){
		if(user==null&&business==null){
			next('/login');
		}
		else{
			next();
		}
	}
    // 检查当前路由是否在公共路径列表中或是否匹配特定的动态路径
    else if (!publicPaths.includes(to.path) && !to.path.match(/^\/edit-food\/\d+$/)) {
        if (user == null) {
            next('/login');
        }
		else {
			next();
		}
    } else {
        next();
    }
});
app.mount('#app');